var utils = {
    // 接口api
    baseUrl: window.location.href.indexOf("/media-audit-h5/")>-1
    || window.location.href.indexOf("127.0.0.1") > -1
    ? 'https://mixcms.qtv.com.cn/dataReport/':'https://mixcms.qtv.com.cn/testdataReport/',

    getMaToken() {
        let MAToken = sessionStorage.getItem("MAToken");
        // let MAToken = utils.getCookie("MAToken");
        if (!utils.isEmpty(MAToken)) {
            return MAToken;
        }
        let imCode = utils.getQueryString("code");
        if (utils.isEmpty(imCode)) {
            utils.customDialog.toast({
                message: '如流code不存在,请刷新页面'
            });
            return null;
        }
        // alert(imCode);
        // return false
        //需要掉登录接口
        $.ajax({
            type: 'post',
            url: utils.baseUrl + 'systemLogin/imH5Login',
            dataType: 'json',
            data: {
                code: imCode
            },
            async: false,
            success: function (res) {
                if (res.code == 200) {
                    MAToken = res.result.MAToken;
                    // utils.addCookie("MAToken", MAToken);
                    sessionStorage.setItem("MAToken", MAToken);

                } else {
                    utils.customDialog.toast({
                        message: res.message
                    });
                }
            }
        })
        return MAToken;
    },
    jumpFunction: function (e) {

        if(e.code == "500404"){
            utils.customDialog.toast({
                message: e.message+"；请关闭页面重新打开"
            });
        }else{
            utils.customDialog.toast({
                message: e.message
            });
        }
    },
    //获取url链接参数
    getQueryString: function (name) {
        var parameters = window.location.search.substr(1).replace("/?/g", "/&/");
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = parameters.match(reg);
        if (r != null) return r[2];
    },
    //添加Cookie
    addCookie: function (name, value, expireHours) {
        var cookieString = name + "=" + escape(value) + "; path=/";
        //判断是否设置过期时间
        if (expireHours > 0) {
            var date = new Date();
            date.setTime(date.getTime() + expireHours * 3600 * 1000);
            cookieString = cookieString + ";expires=" + date.toGMTString();
        }
        document.cookie = cookieString;
    },
    //获取Cookie
    getCookie: function (name) {
        var strcookie = document.cookie;
        var arrcookie = strcookie.split("; ");
        for (var i = 0; i < arrcookie.length; i++) {
            var arr = arrcookie[i].split("=");
            if (arr[0] == name) return unescape(arr[1]);
        }
        return null;
    },
    //删除Cookie
    delCookie: function (name) {
        this.addCookie(name, 1, -1)
    },
    //判断对象是否为空
    isEmpty: function (obj) {
        for (var name in obj) {
            return false;
        }
        return true;
    },
    formatTimeEn: function (t) {
        var date = new Date(t);
        var y = date.getFullYear(),
            m = ('00' + (date.getMonth() + 1)).slice(-2),
            d = ('00' + date.getDate()).slice(-2),
            h = ('00' + date.getHours()).slice(-2),
            mi = ('00' + date.getMinutes()).slice(-2),
            s = ('00' + date.getSeconds()).slice(-2);
        return (y + '-' + m + '-' + d + ' ' + h + ':' + mi + ':' + s)
    },
    /*
       * message 错误提示信息
       * duration 提示持续时间
       * icon 项目展示图标
       */
    customDialog: {
        setStyle: function () {

            var style = document.createElement('style');
            var stylesheet = `#modal-toast {
                          position: fixed;
                          top: 0;
                          left: 0;
                          background-color: transparent;
                          width: 100%;
                          height: 100%;
                          z-index: 10000;
                        }
                        #modal-toast .modal-toast-body {
                          background: rgb(63 60 60 / 90%);
                          color: #fff;
                          display: flex;
                          flex-direction: column;
                          align-items: center;
                          justify-content: center;
                          border-radius: 4px;
                        }
                        #modal-toast .modal-toast-body .contents {
                          font-size: 0.28rem;
                          max-width: 6rem;
                          word-break: break-all;
                          color: #FFFFFF;
                          opacity: 1;
                          text-align: center;
                        }
                        #modal-toast .modal-toast-body.toastIcon {
                          position: absolute;
                          top: 0;
                          left: 0;
                          bottom: 0;
                          right: 0;
                          margin: auto;
                          width: 2.4rem;
                          height: 2.4rem;
                        }
                        #modal-toast .modal-toast-body.toastIcon .icon {
                          margin-bottom: 0.24rem;
                        }
                        #modal-toast .modal-toast-body.toastIcon .icon img {
                          width: 0.72rem;
                          height: 0.72rem;
                        }
                        #modal-toast .modal-toast-body.toast {
                          position: absolute;
                          padding: 0.2rem;
                          left: 50%;
                          top: 50%;
                          transform: translate(-50%, -50%);
                          z-index:999999;
                        }`
            style.innerHTML = stylesheet;
            document.head.appendChild(style);
        },
        toast: function ({ message, duration, icon }) {

            var _this = this;
            var icon = icon || '';
            var duration = duration || 2000;
            if (document.getElementById('modal-toast')) {
                var modal = document.getElementById('modal-toast');
                document.getElementById('modal-toast').style.display = 'block';
                if (icon != '') {
                    modal.innerHTML = `<div class="modal-toast-body toastIcon">
                                    <div class="icon"><img src="images/dialog/${icon}.png" alt=""></div>
                                    <div class="contents">${message}</div>
                                  </div>`
                } else {
                    modal.innerHTML = `<div class="modal-toast-body toast">
                                    <div class="contents">${message}</div>
                                  </div>`
                }
            } else {
                var modal = document.createElement('div');
                modal.id = 'modal-toast';
                if (icon != '') {
                    modal.innerHTML = `<div class="modal-toast-body toastIcon">
                                    <div class="icon"><img src="images/dialog/${icon}.png" alt=""></div>
                                    <div class="contents">${message}</div>
                                  </div>`
                } else {
                    modal.innerHTML = `<div class="modal-toast-body toast">
                                    <div class="contents">${message}</div>
                                  </div>`
                }
                document.body.appendChild(modal);
            }
            setTimeout(function () {
                _this.hideToast()
            }, duration)
        },
        hideToast: function () {
            document.getElementById('modal-toast').style.display = 'none';
        },
        init: function () {
            this.setStyle();
        }
    },
}
utils.customDialog.init()



